package com.skh.stack;

import java.util.Stack;

/**
 * @Author: skh
 * @Date: 2019/4/15 14:08
 * @Description:
 */
public class Solution {

    /*
    用两个栈来实现一个队列，完成队列的Push和Pop操作。
     */
    private Stack<Integer> stack1 = new Stack<>(); //用于入队
    private Stack<Integer> stack2 = new Stack<>(); //用于出队

    public void push(int node) {
        stack1.push(node);
    }

    public int pop() {
        //判断stackB是否有元素,若有直接pop
        //若没有元素,则将stackA中的元素全部push到stockB中
        if (stack2.isEmpty()) {
            if (stack1.isEmpty()) {
                throw new RuntimeException("元素不存在");
            }
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }

        Integer pop = stack2.pop();
        return pop;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.push(1);
        solution.push(2);
        solution.push(3);
        System.out.println("pop = " + solution.pop());
        System.out.println("pop = " + solution.pop());
        System.out.println("pop = " + solution.pop());
    }

}
